
#include <math.h>

#include "ChangeDetection/include/ChangeDetectionWorldObserver.h"
#include "ChangeDetection/include/ChangeDetectionSharedData.h"

ChangeDetectionWorldObserver::ChangeDetectionWorldObserver(World *__world) : WorldObserver(__world) {
	_world = __world;
}

ChangeDetectionWorldObserver::~ChangeDetectionWorldObserver() {
	// nothing to do.
}

void ChangeDetectionWorldObserver::showOrganisms() {
}

void ChangeDetectionWorldObserver::reset() {
	if(ChangeDetectionSharedData::outputLogFile){
		ChangeDetectionSharedData::outputLogFile.close();
	}
	ChangeDetectionSharedData::outputLogFile.open(ChangeDetectionSharedData::gOutputLogFilename.c_str()); //, std::ofstream::out | std::ofstream::app);
	if (!ChangeDetectionSharedData::outputLogFile) {
		std::cout << "[error] Cannot open log file" << ChangeDetectionSharedData::gOutputLogFilename <<"." << std::endl << std::endl;
		exit(1);
	}
	ChangeDetectionSharedData::outputLogFile << "id,timestep,outputs,choice,desired organism size,direction,speed" << std::endl;
	
	
	if(ChangeDetectionSharedData::locationLogFile){
		ChangeDetectionSharedData::locationLogFile.close();
	}
	ChangeDetectionSharedData::locationLogFile.open(ChangeDetectionSharedData::gLocationLogFilename.c_str()); //, std::ofstream::out | std::ofstream::app);
	if (!ChangeDetectionSharedData::locationLogFile) {
		std::cout << "[error] Cannot open log file" << ChangeDetectionSharedData::gLocationLogFilename <<"." << std::endl << std::endl;
		exit(1);
	}
	ChangeDetectionSharedData::locationLogFile << "id,timestep,x,y,in org,org_size,swarm time,organism time" << std::endl;

	//new, for cluster logging:
	if(ChangeDetectionSharedData::clusterLogLogFile){
			ChangeDetectionSharedData::clusterLogLogFile.close();
	}
	ChangeDetectionSharedData::clusterLogLogFile.open(ChangeDetectionSharedData::gClusterLogLogFilename.c_str());
	if (!ChangeDetectionSharedData::clusterLogLogFile) {
			std::cout << "[error] Cannot open log file" << ChangeDetectionSharedData::gClusterLogLogFilename <<"." << std::endl << std::endl;
			exit(1);
	}
	ChangeDetectionSharedData::clusterLogLogFile << "id,iteration,x,y,wrapcount,block,cluster,clustercount" << std::endl;

	//new, for travel tracking:
	if(ChangeDetectionSharedData::travelTrackingFile){
			ChangeDetectionSharedData::travelTrackingFile.close();
	}
	ChangeDetectionSharedData::travelTrackingFile.open(ChangeDetectionSharedData::gTravelTrackingFilename.c_str());
	if (!ChangeDetectionSharedData::travelTrackingFile) {
			std::cout << "[error] Cannot open log file" << ChangeDetectionSharedData::gTravelTrackingFilename <<"." << std::endl << std::endl;
			exit(1);
	}
	ChangeDetectionSharedData::travelTrackingFile << "id,iteration,x,y,clustercount,wrapcount,block,distancetraveled,lastfitness" << std::endl;
}

void ChangeDetectionWorldObserver::step() {

}
